<?php

namespace App\Http\Controllers\Collect;

use App\Http\Controllers\Spider\RulesController;

class CollectController extends RulesController
{
    private $domin = "https://www.360kan.com";

    protected function Index($type)
    {
        if ($type == "indexbanner") {
            $url = "http://www.360kan.com/dianying/index.html";
        } else {
            $url = $this->domin;
        }
        $rules = $this->GetRules("rules", '360kan', $type);
        $data = $this->Collect($url, $rules);
        foreach ($data as $key => $v) {
            $href = base64_decode($v['href']);
            if (str_contains($href, 'special')) {
                unset($data[$key]);
            };
        }
        return $data;
    }

    protected function Kan($type, $cat = null, $pg = null)
    {
        $url = $this->domin . "/" . $type . "/list.php?year=all&area=all&act=all&cat=" . $cat . "&pageno=" . $pg;
        $rules = $this->GetRules("rules", '360kan', $type);
        $data = $this->Collect($url, $rules, '[class="s-tab-main"]>ul>li');
        return $data;
    }

    protected function Search($key)
    {
        $url = 'https://so.360kan.com/index.php?kw=' . $key;
        $rules = $this->GetRules("rules", '360kan', 'search');
        $data = $this->Collect($url, $rules);
        $data2 = $this->PostApi($key);
        if (is_array($data2)) {
            return array_merge($data, $data2);
        } else {
            return $data;
        }

    }


    protected function Play($type, $url)
    {
        switch ($type) {
            case "kan":
                $url = base64_decode($url);
                $data = $this->Kanplay($url);
                return $data;
                break;
            case "zy":
                $zy = explode('-', $url);
                $data = $this->PostApi('', $zy[0], $zy[1]);
                $data['type'] = "zy";
                return $data;
                break;
        }
    }

    protected function BdList($type, $range = "")
    {
        $url = 'https://www.360kan.com/' . $type . '/index.html';
        $rules = $this->GetRules("rules", '360kan', $type . 'bd');
        $data = $this->Collect($url, $rules, $range);
        return $data;
    }

    private function Kanplay($url)
    {
        $type = $this->CheckType($url);
        switch ($type) {
            case "m":
                $rules = $this->GetRules("rules", '360kan', 'dyplay');
                $rules2 = $this->GetRules("rules", "360kan", 'dylike');
                $data = $this->Collect($this->domin . $url, $rules);
                $data['like'] = $this->Collect($this->domin . $url, $rules2);
                $data['type'] = "dianying";
                return $data;
                break;
            case "va":
                $rules = $this->GetRules("rules", '360kan', 'zyplay');
                $rules2 = $this->GetRules("rules", "360kan", 'zylike');
                $data = $this->Collect($this->domin . $url, $rules);
                $data['like'] = $this->Collect($this->domin . $url, $rules2);
                $data['type'] = "zongyi";
                return $data;
                break;
            case "tv":
                $rules = $this->GetRules("rules", '360kan', 'tvplay');
                $rules2 = $this->GetRules("rules", "360kan", 'tvlike');
                $data['info'] = ($this->Collect($this->domin . $url, $rules))[0];
                $data['like'] = $this->Collect($this->domin . $url, $rules2);
                $data['playlist'] = $this->GetTvData($url, 2);
                $data['type'] = "dianshi";
                return $data;
                break;
            case "ct":
                $rules = $this->GetRules("rules", '360kan', 'dmplay');
                $rules2 = $this->GetRules("rules", "360kan", 'dmlike');
                $data['info'] = ($this->Collect($this->domin . $url, $rules))[0];
                $data['like'] = $this->Collect($this->domin . $url, $rules2);
                $data['playlist'] = $this->GetTvData($url, 4);
                $data['type'] = "dongman";
                return $data;
                break;
        }

    }

    private function CheckType($url)
    {
        $arr = explode('/', $url);
        return $arr[1];
    }

    protected function DealPlay($arr)
    {
        if (!is_array($arr)) {
            $varr[] = $arr;
            $arr = $varr;
        }
        foreach ($arr as $key => $v) {
            $arr2 = explode('#', $v);
            if (strpos($arr2[0], 'm3u8')) {
                $arr3[$key]['name'] = 'm3u8';
            } elseif (strpos($arr2[0], 'mp4')) {
                $arr3[$key]['name'] = 'mp4';
            } else {
                $arr3[$key]['name'] = 'zhilian';
            }
            foreach ($arr2 as $k) {
                $arr3[$key]['data'][] = explode('$', $k);
            }
        }
        return $arr3;
    }


}